package com.example.wallet

import com.example.wallet.bean.AccountInfo
import com.example.wallet.db.AccountInfoManager
import com.example.wallet.utils.createWalletChildFile
import io.github.novacrypto.bip39.MnemonicGenerator
import io.github.novacrypto.bip39.SeedCalculator
import io.github.novacrypto.bip39.Words
import io.github.novacrypto.bip39.wordlists.English
import org.web3j.crypto.Keys
import org.web3j.crypto.Wallet
import org.web3j.crypto.WalletUtils
import java.security.SecureRandom


fun createAccount(): AccountInfo {
    val keyPair = Keys.createEcKeyPair()
    val privateKey = keyPair.privateKey.toString(16)
    val publicKey = keyPair.publicKey.toString(16)
    val account = Keys.getAddress(keyPair)
    return AccountInfo(publicKey, account, privateKey)
}

/**
 * generate a random group of mnemonics
 * 生成一组随机的助记词
 */
  fun generateMnemonics(): String? {
    val sb = StringBuilder()
    val entropy = ByteArray(Words.TWELVE.byteLength())
    SecureRandom().nextBytes(entropy)
    MnemonicGenerator(English.INSTANCE)
        .createMnemonic(entropy) { s: CharSequence? -> sb.append(s) }
    return sb.toString()
}


//备份KeysStore
fun saveKeysStore(passWord: String){

    SeedCalculator().calculateSeed(generateMnemonics(), passWord)

    val file = createWalletChildFile()
    var accountInfo = AccountInfoManager.INSTANCE.getAccountInfo()
//    WalletUtils.generateWalletFile()
}